package com.itfreer.gis.map.style.transfer;

import java.util.LinkedHashMap;
import java.util.Map;

import com.itfreer.utils.BaseHost;

/**
 * 定义飞码地图样式转换
 */
public class FmStyleTransfer {

	/**
	 * 飞码地图样式转换
	 * 
	 * @param json
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> transferEx(Map<String, Object> styleMap, BaseHost baseHost) {
		String glyphs = styleMap.get("glyphs").toString();
		glyphs = glyphs.replace("fm://", baseHost.getBaseUrl());
		glyphs = glyphs.replace("localhost", "10.0.2.2");
		if (!glyphs.endsWith(".pbf")) {
			glyphs += ".pbf";
		}
		styleMap.put("glyphs", glyphs);

		String sprite = styleMap.get("sprite").toString();
		sprite = sprite.replace("localhost", "10.0.2.2");
		styleMap.put("sprite", sprite);

		Map<String, Object> sources = (Map<String, Object>) styleMap.get("sources");
		Map<String, Object> mbSources = new LinkedHashMap<>();
		for (String key : sources.keySet()) {
			Map<String, Object> mbSource = transfer((Map<String, Object>) sources.get(key));
			if (mbSource != null) {
				mbSources.put(key, mbSource);
			}
		}
		styleMap.put("sources", mbSources);
		return styleMap;
	}

	private static Map<String, Object> transfer(Map<String, Object> source) {
		Object serverType = source.get("serverType");
		if (isHandle(serverType)) {
			// 需要处理source对象
			return transferSource(source);
		} else {
			return source;
		}
	}

	private static Map<String, Object> transferSource(Map<String, Object> source) {
		String serverType = source.get("serverType").toString();
		switch (serverType) {
		case ServerTypeEnum.ARC_WMS: // Arcgis WMS
			source = MbArcLayer.WMS(source);
			break;
		case ServerTypeEnum.ARC_WMTS: // Arcgis WMTS
			source = MbArcLayer.WMTS(source);
			break;
		case ServerTypeEnum.ARC_WFS: // Arcgis WFS
			source = MbArcLayer.WFS(source);
			break;
		case ServerTypeEnum.ARC_TMS: // Arcgis 矢量切片
			source = MbArcLayer.TMS(source);
			break;
		case ServerTypeEnum.GEO_WMS: // Geoserver WMS
			source = MbGeoLayer.WMS(source);
			break;
		case ServerTypeEnum.GEO_WMTS: // Geoserver WMTS
			source = MbGeoLayer.WMTS(source);
			break;
		case ServerTypeEnum.GEO_WFS: // Geoserver WFS
			source = MbGeoLayer.WFS(source);
			break;
		case ServerTypeEnum.GEO_TMS: // Geoserver 矢量切片
			source = MbGeoLayer.TMS(source);
			break;
		case ServerTypeEnum.SUP_WMS: // SuperMap WMS
			source = MbSupermpLayer.WMS(source);
			break;
		case ServerTypeEnum.SUP_WMTS: // SuperMap WMTS
			source = MbSupermpLayer.WMTS(source);
			break;
		case ServerTypeEnum.SUP_WFS: // SuperMap WFS
			source = MbSupermpLayer.WFS(source);
			break;
		case ServerTypeEnum.SUP_TMS: // SuperMap 矢量切片
			source = MbSupermpLayer.TMS(source);
			break;
		default:
			throw new Error("Unsupported source  engine: " + serverType);
		}
		;
		return source;
	}

	/**
	 * 判断是否需要处理source对象
	 */
	private static boolean isHandle(Object serverType) {
		boolean isHandle = false;
		if (serverType == null || serverType.equals("")) {
			return isHandle;
		}
		String st = serverType.toString();
		switch (st) {
		case ServerTypeEnum.ARC_WMS: // Arcgis WMS
		case ServerTypeEnum.ARC_WMTS: // Arcgis WMTS
		case ServerTypeEnum.ARC_WFS: // Arcgis WFS
		case ServerTypeEnum.ARC_TMS: // Arcgis 矢量切片
		case ServerTypeEnum.GEO_WMS: // Geoserver WMS
		case ServerTypeEnum.GEO_WMTS: // Geoserver WMTS
		case ServerTypeEnum.GEO_WFS: // Geoserver WFS
		case ServerTypeEnum.GEO_TMS: // Geoserver 矢量切片
		case ServerTypeEnum.SUP_WMS: // SuperMap WMS
		case ServerTypeEnum.SUP_WMTS: // SuperMap WMTS
		case ServerTypeEnum.SUP_WFS: // SuperMap WFS
		case ServerTypeEnum.SUP_TMS: // SuperMap 矢量切片
			isHandle = true;
			break;
		}
		return isHandle;
	}
}
